<template>
	<view>
		<view class="padding text-red ">
			请根据您的需要,
			自定义配置产品参数，系统自动为您
			核算产品价格!
		</view>
		<view class="rowsb align-center padding bg-white margin-bottom-xs">
			<view>{{productTypeList[0].name}}</view>
			<view>
				<text>{{productTypeList2[0].name ||'请选择'}}</text>

			</view>
		</view>
		<view class="rowsb align-center padding bg-white margin-bottom-xs">
			<view>{{productTypeList[1].name}}</view>
			<view class="align-center" v-for="(item,index) in productType1" :key='index'>
				<view class="margin-left-sm">{{item.name}}</view>
			</view>
		</view>
		<!-- @click="zAxleMen()" -->
		<view class="rowsb align-center padding bg-white margin-bottom-xs">
			<view>{{productTypeList[2].name}}</view>
			<view><text>{{productTypeList2[2].name ||'请选择'}}</text></view>
		</view>
		<!-- @click="kernelAxleMen()" -->
		<view class="rowsb align-center padding bg-white margin-bottom-xs">
			<view>{{productTypeList[3].name}}</view>
			<view>
				<text>选择主轴:</text>
				<text>{{productTypeList2[3].name ||'请选择'}}</text>
				<text v-if="productTypeList2[3].num">数量:{{productTypeList2[3].num}}</text>
			</view>
		</view>
		<view class="bg-white padding margin-bottom-xs">
			<view class="rowsb align-center">
				<text>{{productTypeList[4].name}}</text>
				<text class="margin-lr-sm">{{jNum}}</text>
			</view>
			<view v-for="ite,indx in 0" :key='indx' class=" padding-bottom">
				<view class="flex bg-white padding-tb" v-for="(item,index) in productType4" :key='index'>
					<text>{{item.name}}</text>
					<view class="margin-left">
						<view v-for="(item01,index01) in item.subordinate" :key='index'
							class="align-center margin-bottom-xs" @click="sawWebMen(item,index01)">
							<image :src="item01.num==index01?'/static/icon/xz.png':'/static/icon/wxz.png'"
								class="is_select">
							</image>
							<text class="margin-left-xs">{{item01.name}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- @click="electricDriveMen()"> -->
		<view class="rowsb align-center padding bg-white margin-bottom-xs">
			<view>{{productTypeList[5].name}}</view>
			<view><text>{{productTypeList2[5].name ||'请选择'}}</text></view>
		</view>
		<!-- @click="transmissionMen()"> -->
		<view class="rowsb align-center padding bg-white margin-bottom-xs">
			<view>{{productTypeList[6].name}}</view>
			<view><text>{{productTypeList2[6].name ||'请选择'}}</text></view>
		</view>
		<view class="rowsb align-center padding bg-white margin-bottom-xs">
			<view>{{productTypeList[7].name}}</view>
			<view class="align-center" v-for="(item,index) in productType7" :key='index'>
				<view class="margin-left-sm">{{item.name}}</view>
			</view>
		</view>

		<!-- @click="spinAxleMen()" -->
		<view class="rowsb align-center padding bg-white margin-bottom-xs">
			<view>{{productTypeList[8].name}}</view>
			<view><text>{{productTypeList2[8].name ||'请选择'}}</text></view>
		</view>
		<view class="rowsb align-center padding bg-white margin-bottom-xs">
			<view>{{productTypeList[9].name}}</view>
			<view><text>{{productTypeList2[9].name ||'请选择'}}</text></view>
		</view>
		<view class="rowsb align-center padding bg-white margin-bottom-xs">
			<view>{{productTypeList[10].name}}</view>
			<view class="align-center" v-for="(item,index) in productType10" :key='index'>
				<view class="margin-left-sm">{{item.name}}</view>
			</view>
		</view>
		<!-- @click="elseAxleMen" -->
		<view class="rowsb align-center padding bg-white margin-bottom-xs" v-for="(item,index) in productelseList"
			:key='index'>
			<view>{{item.name}}</view>
			<view><text>{{productTypeList2[index+10].name ||'请选择'}}</text></view>
		</view>
		<view class="cu-bar bg-white tabbar border shop" style="position: fixed;bottom: 0;width: 100%;z-index: 9;">
			<view class=" padding-lr">
				<text>金额：</text>
				<text class="text-red text-xl">￥{{parseInt(price)}}</text>
			</view>
			<view class="bg-blue submit" @click="back()">编辑</view>
			<view class="bg-orange submit" @click="orderShopping()">加入购物车</view>
			<view class="bg-red submit" @click="orderSubmit()">立即订购</view>
		</view>
	</view>
</template>

<script>
	import {
		getProductType,
		placeOrder,
		addProductCat,getPrice
		
	} from '@/common/path.js'
	export default {
		data() {
			return {
				// 列表
				productTypeList: [], //普通
				productelseList: [], //其他
				productTypeList2: [], //选择的数据
				//电压列表
				productType1: [],
				isVoltage: 9999,
				//活动台面
				productType7: [],
				isVoltage7: 9999,
				//据片数量
				jNum: 0,
				jNumList: [],
				//控制柜
				productType10: [],
				isVoltage10: 9999,
				//总价格
				total: 0.00,
				//锯片
				productType4: [],
				//商品数量
				num: 1,
				
				price:''
			}
		},
		onLoad(options) {
			//总价格
			this.total = options.total
			//购买数量
			this.num = options.num
			// 锯片数量
			this.jNum = options.jNum
			console.log('[锯片数量]', options.jNum);
			//分类
			this.getProductType()

		},
		onShow() {
			this.productTypeList2 = uni.getStorageSync('productTypeList')
			this.isVoltage = this.productTypeList2[1].index
			this.isVoltage7 = this.productTypeList2[7].index
			this.isVoltage10 = this.productTypeList2[10].index


			this.productType7 = []
			this.productType1 = []
			this.productType10 = []

			this.productType1.push(this.productTypeList2[1])
			this.productType7.push(this.productTypeList2[7])
			this.productType10.push(this.productTypeList2[10])
			
			this.getPrice()
		},

		methods: {
			//价格
			getPrice(){
				let _self, data
				_self = this
				let _t = this.$tools
				let ids = []
				let num = 0
				let idstring = []
				let id01 = uni.getStorageSync('arrayList_id01')
				let id02 = uni.getStorageSync('arrayList_id02')
				let productTypeArray = uni.getStorageSync('productTypeList')
				productTypeArray.forEach(item => {
					if (item == null) {
						let obj = {
							id: 0
						}
						item = obj
					}
					ids.push(item.id ? item.id : 0)
				})
				
				// if (uni.getStorageSync('productTypeList')[4] == null) { //如果没有选择锯片数量
				// 	num = 0
				// 	idstring = [...ids.slice(0, 5), 0, 0, ...ids.slice(5, ids.length)]
				// } else {
				// 	num = uni.getStorageSync('productTypeList')[4].num
				// 	idstring = [...ids.slice(0, 5), id01, id02, ...ids.slice(5, ids.length)]
				// }
				
				
				if (uni.getStorageSync('productTypeList')[4] == null) { //如果没有选择锯片数量
					num = 0
					//带 其他类型的 
					// idstring = [...ids.slice(0, 5), 0, 0, ...ids.slice(5, ids.length)]
					idstring = [...ids.slice(0, 5), 0, 0, ...ids.slice(5, 11)]
				} else {
					num = uni.getStorageSync('productTypeList')[4].num
					//带 其他类型的
					// idstring = [...ids.slice(0, 5), id01, id02, ...ids.slice(5, ids.length)]
					
					
					idstring = [...ids.slice(0, 5), id01, id02, ...ids.slice(5, 11)]
				}
				
				
				console.log('1',productTypeArray[3].num);
				data = {
					string: idstring.toString(),
					main_number: productTypeArray[3].num, //主轴数量
					number: this.num, //产品数量
					blades_number: uni.getStorageSync('productTypeList')[4] ? uni.getStorageSync(
						'productTypeList')[4].num : 0
				}
				 
				 
				// console.log('[穿的参数]', data);
				this.$axios.axios('POST', getPrice, data)
					.then((res) => {
						console.log('[价格]', res);
						if (res.code == 1) {
							this.price = res.data.price
							
						} else {
							_t.showToast(res.msg)
						}
					})
				
			},
			
			//编辑配置
			back() {
				uni.navigateBack({
					delta: 1
				})
			},


			//加入购物车
			orderShopping() {
				let _self, data
				_self = this
				let _t = this.$tools
				let ids = []
				let productTypeArray = uni.getStorageSync('productTypeList')

				let num = 0
				let idstring = []
				let id01 = uni.getStorageSync('arrayList_id01')
				let id02 = uni.getStorageSync('arrayList_id02')


				productTypeArray.forEach(item => {
					if (item == null) {
						let obj = {
							id: 0
						}
						item = obj
					}
					ids.push(item.id ? item.id : 0)
				})
				// if (uni.getStorageSync('productTypeList')[4] == null) { //如果没有选择锯片数量
				// 	num = 0
				// 	idstring = [...ids.slice(0, 5), 0, 0, ...ids.slice(5, ids.length)]
				// } else {
				// 	num = uni.getStorageSync('productTypeList')[4].num
				// 	idstring = [...ids.slice(0, 5), id01, id02, ...ids.slice(5, ids.length)]
				// }
				
				
				if (uni.getStorageSync('productTypeList')[4] == null) { //如果没有选择锯片数量
					num = 0
					//带 其他类型的 
					// idstring = [...ids.slice(0, 5), 0, 0, ...ids.slice(5, ids.length)]
					idstring = [...ids.slice(0, 5), 0, 0, ...ids.slice(5, 11)]
				} else {
					num = uni.getStorageSync('productTypeList')[4].num
					//带 其他类型的
					// idstring = [...ids.slice(0, 5), id01, id02, ...ids.slice(5, ids.length)]
					
					
					idstring = [...ids.slice(0, 5), id01, id02, ...ids.slice(5, 11)]
				}
				
				console.log('[]');
				data = {
					string: idstring.toString(),
					main_number: productTypeArray[3].num, //主轴数量
					number: this.num, //产品数量
					blades_number: uni.getStorageSync('productTypeList')[4] ? uni.getStorageSync(
						'productTypeList')[4].num : 0
				}
				uni.showLoading({
					'title': '处理中'
				})

				this.Http.POST(addProductCat, data)
					.then((res) => {
						if (res.code == 1) {
							setTimeout(r => {
								uni.hideLoading()
								uni.showModal({
									title: '温馨提示',
									content: '已添加到购物车请自行查看',
									success: function(Res) {
										if (Res.confirm) {
											setTimeout(r=>{
												uni.reLaunch({
													url:'/pages/goods/shopping'
												})
											},1000)
										} else if (Res.cancel) {
											console.log('用户点击取消');
										}
									}
								})
							}, 1500)
							console.log('[购物车添加成功]', res);
						} else {
							this.$tools.showToast(res.msg)
						}
					})
			},
			// 提交订单
			orderSubmit() {
				let _self, data
				_self = this
				let _t = this.$tools
				let ids = []
				let num = 0
				let idstring = []
				let id01 = uni.getStorageSync('arrayList_id01')
				let id02 = uni.getStorageSync('arrayList_id02')
				let productTypeArray = uni.getStorageSync('productTypeList')
				productTypeArray.forEach(item => {
					if (item == null) {
						let obj = {
							id: 0
						}
						item = obj
					}
					ids.push(item.id ? item.id : 0)
				})

				// if (uni.getStorageSync('productTypeList')[4] == null) { //如果没有选择锯片数量
				// 	num = 0
				// 	idstring = [...ids.slice(0, 5), 0, 0, ...ids.slice(5, ids.length)]
				// } else {
				// 	num = uni.getStorageSync('productTypeList')[4].num
				// 	idstring = [...ids.slice(0, 5), id01, id02, ...ids.slice(5, ids.length)]
				// }
				
				if (uni.getStorageSync('productTypeList')[4] == null) { //如果没有选择锯片数量
					num = 0
					//带 其他类型的 
					// idstring = [...ids.slice(0, 5), 0, 0, ...ids.slice(5, ids.length)]
					idstring = [...ids.slice(0, 5), 0, 0, ...ids.slice(5, 11)]
				} else {
					num = uni.getStorageSync('productTypeList')[4].num
					//带 其他类型的
					// idstring = [...ids.slice(0, 5), id01, id02, ...ids.slice(5, ids.length)]
					
					
					idstring = [...ids.slice(0, 5), id01, id02, ...ids.slice(5, 11)]
				}
				

				data = {
					string: idstring.toString(),
					main_number: productTypeArray[3].num, //主轴数量
					number: this.num, //产品数量
					blades_number: uni.getStorageSync('productTypeList')[4] ? uni.getStorageSync(
						'productTypeList')[4].num : 0
				}
				console.log('[穿的参数]', data);
				this.$axios.axios('POST', placeOrder, data)
					.then((res) => {
						console.log('[成功]', res);
						if (res.code == 1) {
							_t.showToast2(res.msg)
							setTimeout(r=>{
								uni.reLaunch({
									url:'/pages/goods/shopping'
								})
							},1000)
						} else {
							_t.showToast(res.msg)
						}
					})

			},
			//获取产品分类
			getProductType1(pid, index) {
				let data = {
					pid: pid
				}
				this.$axios.axios('POST', getProductType, data)
					.then((res) => {
						if (index == 4) {
							res.data.forEach(item => {
								item.subordinate.forEach(item01 => {
									item01.num = 0
								})
							})
							this.productType4 = res.data
						}
					})
			},

			//分类
			getProductType() {
				let data = {
					pid: 0,
				}
				this.$axios.axios('POST', getProductType, data)
					.then((res) => {
						if (res.code == 1) {
							this.productTypeList = res.data
							let idList = [{
									id: res.data[4].id,
									index: 4
								},

							]
							idList.forEach(item => {
								this.getProductType1(item.id, item.index)
							})
							this.productelseList = res.data.splice(11)
						}
					})
			},

		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f7f7f7;
		padding-bottom: 120rpx;
	}

	.jupian_icon {
		height: 50rpx;
		width: 50rpx;
	}

	.is_select {
		width: 40rpx;
		height: 40rpx;
	}
</style>
